<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
  PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="${package}.${module.packageName}.dao.${module.name}Dao">

	<resultMap type="${package}.${module.packageName}.service.${module.name}" id="${module.name?uncap_first}">
		<#list module.fields as field>
		<#if (field.constraint)?? && field.constraint.primaryKey>
		<id column="${field.field}" jdbcType="${field.type.jdbcType}" property="${field.name}"/>
		<#else>
		<result column="${field.field}" jdbcType="${field.type.jdbcType}" property="${field.name}"/>
		</#if>
		</#list>
		<@point name="XML:MYBATIS:RESULT_MAP:ASSOCIATION" />
	</resultMap>
	
	<sql id="queryFields">
	<#list module.fields as field>
		${field.field}<#if field_has_next>,</#if>
	</#list>
	</sql>
	
	<insert id="add${module.name}">
		<selectKey keyProperty="${module.pkField.name}" resultType="String" order="BEFORE" databaseId="mysql">
			SELECT uuid() FROM dual
		</selectKey>
		<selectKey keyProperty="${module.pkField.name}" resultType="String" order="BEFORE" databaseId="oracle">
			select sys_guid() from dual
		</selectKey>
		INSERT INTO ${module.tableName} (
		<#list module.fields as field>${field.field}<#if field_has_next>,</#if></#list><#if extParam.GEN_DEFAULT_FIELD == true>,CREATE_DATE,INVALID_DATE,CREATE_USER,STATE</#if>
		)VALUES(
		<#list module.fields as field>${"#{"+field.name+"}"}<#if field_has_next>,</#if></#list><#if extParam.GEN_DEFAULT_FIELD == true>,${"#"+"{createDate}"},${"#"+"{invalidDate}"},${"#"+"{createUser}"},${"#"+"{state}"}</#if>
		)
	</insert>
	
	<#if !extParam.IS_LOGIC_DELETE>
	<delete id="delete${module.name}">
		DELETE FROM ${module.tableName} WHERE ${module.pkField.field} IN
		<foreach item="id" index="index" collection="ids" open="(" separator="," close=")">
		  ${"#"+"{id}"}
		</foreach>
	</delete>
	</#if>
	<#if extParam.IS_LOGIC_DELETE && extParam.IS_LOGIC_DELETE>
	<delete id="delete${module.name}">
		UPDATE ${module.tableName} SET IS_ENABLE=2 WHERE ${module.pkField.field} IN
		<foreach item="id" index="index" collection="ids" open="(" separator="," close=")">
		  ${"#"+"{id}"}
		</foreach>
	</delete>
	</#if>
	
	<update id="update${module.name}">
		UPDATE ${module.tableName}
		<set>
			<#list module.fields as field>
				<#if (field.constraint)?? && !field.constraint.primaryKey>
			<if test="${field.name} != null <#if field.type.simpleJavaType=="String">and ${field.name} != ''</#if>">${field.field} = ${'#{'+field.name+"}"},</if>
				</#if>
			</#list>
		</set>
		WHERE ${module.pkField.field} = ${'#{'+ module.pkField.name + "}"}
	</update>
	
	<select id="get${module.name}" resultMap="${module.name?uncap_first}">
		SELECT <include refid="queryFields"/> from ${module.tableName} WHERE ${module.pkField.field} = ${"#"+"{id}"}
		<#if extParam.GEN_DEFAULT_FIELD>
			AND STATE = 1
		</#if>
	</select>
	
	<select id="list${module.name}" resultMap="${module.name?uncap_first}">
		SELECT <include refid="queryFields"/> from ${module.tableName}
		<where>
		<#if module.hasQueryFiled>
			<#list module.fields as field>
			<#if (field.constraint.queryType)??>
				<#if (field.constraint.queryType)?? >
				<#if field.constraint.queryType == 'RANGE'>
			<if test="query.search${field.name ? cap_first}Start != null">
				AND ${field.field} &gt;= ${'#\{query.search'+(field.name?cap_first) + 'Start}'}
			</if>
			<if test="query.search${field.name ? cap_first}End != null">
				AND ${field.field} &lt;= ${'#\{query.search'+(field.name?cap_first) + 'End}'}
			</if>
				<#else>
			<if test="query.search${field.name ? cap_first} != null <#if field.type.simpleJavaType=="String">and query.search${field.name ? cap_first} != ''</#if>">
				<#if field.constraint.queryType == 'LIKE'>
				<bind name="${field.name}Like" value="'%'+query.search${field.name ? cap_first}+'%'"/>
				<#elseif field.constraint.queryType == 'LEFT_LIKE'>
				<bind name="${field.name}Like" value="'%'+query.search${field.name ? cap_first}"/>
				<#elseif field.constraint.queryType == 'RIGHT_LIKE'>
				<bind name="${field.name}Like" value="query.search${field.name ? cap_first}+'%'"/>
				</#if>
				AND ${field.field} <#if (field.constraint.queryType)?? && (field.constraint.queryType == 'LIKE' || field.constraint.queryType == 'LEFT_LIKE' || field.constraint.queryType == 'RIGHT_LIKE')>LIKE ${'#\{'+field.name + 'Like}'}<#else>=${'#\{query.search'+(field.name?cap_first)+'}'}</#if>
			</if>
				</#if>
				</#if>
			</#if>
			</#list>
		</#if>
			<#if extParam.GEN_DEFAULT_FIELD>
				AND STATE = 1
			</#if>
		</where>
		<#if plugins??>
			<#list plugins as pluginName>
				<#if pluginName == 'order'>
					<#list module.fields as field>
						<#if field.field == 'ORDER_NUM'>
			ORDER BY ORDER_NUM
						</#if>
					</#list>
				</#if>
			</#list>
		</#if>
	</select>
	
	<@point name="XML:MYBATIS:OPERATE" />
	
</mapper>